package com.hbms.business.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.hbms.business.domain.HotelbmsRooms;
import com.hbms.business.mapper.HotelbmsRoomsMapper;
import com.hbms.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hbms.business.mapper.HotelbmsRoomstatusMapper;
import com.hbms.business.domain.HotelbmsRoomstatus;
import com.hbms.business.service.IHotelbmsRoomstatusService;

/**
 * 客房状态Service业务层处理
 *
 * @author zl
 * @date 2024-04-10
 */
@Service
public class HotelbmsRoomstatusServiceImpl implements IHotelbmsRoomstatusService {
    @Autowired
    private HotelbmsRoomstatusMapper hotelbmsRoomstatusMapper;
    @Autowired
    private HotelbmsRoomsMapper hotelbmsRoomsMapper;

    /**
     * 查询客房状态
     *
     * @param roomStatusId 客房状态主键
     * @return 客房状态
     */
    @Override
    public HotelbmsRoomstatus selectHotelbmsRoomstatusByRoomStatusId(Long roomStatusId) {
        return hotelbmsRoomstatusMapper.selectHotelbmsRoomstatusByRoomStatusId(roomStatusId);
    }

    /**
     * 查询客房状态列表
     *
     * @param hotelbmsRoomstatus 客房状态
     * @return 客房状态
     */
    @Override
    public List<Map<String, Object>> selectHotelbmsRoomstatusList(HotelbmsRoomstatus hotelbmsRoomstatus) {
        List<Map<String, Object>> list = new ArrayList<>();
        // 查询所有的房间类型
        List<HotelbmsRooms> hotelbmsRoomsList = hotelbmsRoomsMapper.selectHotelbmsRoomsList(new HotelbmsRooms());
        if (hotelbmsRoomsList == null || hotelbmsRoomsList.size() == 0) {
            throw new ServiceException("该酒店未拥有客房，请先添加！");
        }
        // 查询该类型的所有房间以及状态
        for (HotelbmsRooms hotelbmsRooms : hotelbmsRoomsList) {
            List<HotelbmsRoomstatus> hotelbmsRoomstatuses = hotelbmsRoomstatusMapper.selecthotelbmsRoomAllRoomStatusByRoomId(hotelbmsRooms.getRoomId());
//            if (hotelbmsRoomstatuses == null || hotelbmsRoomstatuses.isEmpty()) {
//                //跳过此次循环
//                continue;
//            }
            Map<String, Object> roomInfoMap = new HashMap<>();
            roomInfoMap.put("roomType", hotelbmsRooms.getRoomType());
            roomInfoMap.put("roomInfo", hotelbmsRooms);
            roomInfoMap.put("roomsDetails", hotelbmsRoomstatuses);
            list.add(roomInfoMap);
        }
        return list;
    }

   /* @Override
    public List<Map<String, Object>> selectHotelbmsRoomstatusList(HotelbmsRoomstatus hotelbmsRoomstatus) {
        List<HotelbmsRooms> hotelbmsRoomsList = hotelbmsRoomsMapper.selectHotelbmsRoomsList(new HotelbmsRooms());

        if (hotelbmsRoomsList == null || hotelbmsRoomsList.isEmpty()) {
            throw new ServiceException("该酒店未拥有客房，请先添加！");
        }

        return hotelbmsRoomsList.stream()
                .map(hotelbmsRooms -> {
                    List<HotelbmsRoomstatus> roomStatuses = hotelbmsRoomstatusMapper.selecthotelbmsRoomAllRoomStatusByRoomId(hotelbmsRooms.getRoomId());
                    if (roomStatuses == null || roomStatuses.isEmpty()) {
                        return null;
                    }
                    Map<String, Object> roomInfoMap = new HashMap<>();
                    roomInfoMap.put("roomType", hotelbmsRooms.getRoomType());
                    roomInfoMap.put("roomInfo", hotelbmsRooms);
                    roomInfoMap.put("roomsDetails", roomStatuses);
                    return roomInfoMap;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }*/





    /**
     * 新增客房状态
     *
     * @param hotelbmsRoomstatus 客房状态
     * @return 结果
     */
    @Override
    public int insertHotelbmsRoomstatus(HotelbmsRoomstatus hotelbmsRoomstatus) {
        return hotelbmsRoomstatusMapper.insertHotelbmsRoomstatus(hotelbmsRoomstatus);
    }

    /**
     * 修改客房状态
     *
     * @param hotelbmsRoomstatus 客房状态
     * @return 结果
     */
    @Override
    public int updateHotelbmsRoomstatus(HotelbmsRoomstatus hotelbmsRoomstatus) {
        return hotelbmsRoomstatusMapper.updateHotelbmsRoomstatus(hotelbmsRoomstatus);
    }

    /**
     * 批量删除客房状态
     *
     * @param roomStatusIds 需要删除的客房状态主键
     * @return 结果
     */
    @Override
    public int deleteHotelbmsRoomstatusByRoomStatusIds(Long[] roomStatusIds) {
        return hotelbmsRoomstatusMapper.deleteHotelbmsRoomstatusByRoomStatusIds(roomStatusIds);
    }

    /**
     * 删除客房状态信息
     *
     * @param roomStatusId 客房状态主键
     * @return 结果
     */
    @Override
    public int deleteHotelbmsRoomstatusByRoomStatusId(Long roomStatusId) {
        return hotelbmsRoomstatusMapper.deleteHotelbmsRoomstatusByRoomStatusId(roomStatusId);
    }
}
